﻿package rob.data{
	import flash.display.Sprite;
	import flash.geom.Point;
	import rob.data.Vert3d;
	import rob.maths.VertexMaths;
	import rob.core.World3d;
	
	// 2D Bounding square sprite, used for buildings etc
	
	public class BSqr extends Sprite{
		public var min:Point, max:Point;
		
		public function BSqr():void{
			min = new Point();
			max = new Point();
			visible = false;
		}
		
		// given an array of vects/verts compute boundaries 
		// and midpoint
		public function compute(a:Array):void{
			min.x = max.x = a[0].sx;
			min.y = max.y = a[0].sy;
			x = y = 0;
			
			for each(var p in a){
				// bounding
				if(p.sx < min.x) min.x = p.sx;
				else if(p.sx > max.x) max.x = p.sx;
				if(p.sy < min.y) min.y = p.sy;
				else if(p.sy > max.y) max.y = p.sy;
				// midpoint
				x += p.sx;
				y += p.sy;
			}
			x /= a.length;
			y /= a.length;
	
			// draw so that required width/height is acheived
			var w:Number = max.x - min.x, h:Number = max.y - min.y;
			// now scale a bit to cover whole building (*APPROX*)
			// could improve by using buildings height...
				var k:Number = 1.5;
				w *= k;
				h *= k;
			
			with(this.graphics){
				clear();
				lineStyle(0,0xFF0000);
				beginFill(0x444444,1);
				moveTo(-w/2,-h/2); 	lineTo(w/2,-h/2);
				lineTo(w/2,h/2);	lineTo(-w/2,h/2);
				lineTo(-w/2,-h/2);
				endFill();
			}
		}
		
		override public function toString():String{
			return "BSqr: ("+min+","+max+")";
		}
	}
}